package cn.waka.sys.admin.controller;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Banner;
import cn.waka.sys.admin.object.Column;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.service.BannerService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/banner")
public class BannerController extends BaseController {

    @Autowired
    private BannerService bannerService;

    @RequestMapping(path = "/banner", name = "广告列表", produces = "text/html; charset=UTF-8")
    public ModelAndView getBannerList(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> list = bannerService.getBannerList();
        ModelAndView mv = new ModelAndView();
        mv.addObject("banners", list);
        return mv;
    }

    @RequestMapping(path = "/update_banner_status", name = "广告显示|隐藏", produces = "application/json; charset=UTF-8")
    public Result isShow(HttpServletRequest req) {
        int id = Integer.parseInt(req.getParameter("id"));
        int status = Integer.parseInt(req.getParameter("status"));

        int rs = bannerService.isShow(id, status);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/index", name = "推荐列表", produces = "text/html; charset=UTF-8")
    public ModelAndView getColumnList(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> list = bannerService.getColumnList();
        ModelAndView mv = new ModelAndView();
        mv.addObject("list", list);
        return mv;
    }

    @RequestMapping(path = "/save", name = "广告添加", produces = "application/json; charset=UTF-8")
    public Result save(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String title = req.getParameter("title");
        String img = req.getParameter("img");
        String resource_idStr = req.getParameter("resource_id");
        int resource_id = 0;
        if (resource_idStr != null && Validator.isNum(resource_idStr)) {
            resource_id = Integer.parseInt(resource_idStr);
        }
        Banner banner = new Banner();
        banner.setId(id);
        banner.setTitle(title);
        banner.setImg(img);
        banner.setResource_id(resource_id);
        banner.setC_time(new Timestamp(System.currentTimeMillis()));
        int rs = bannerService.saveBanner(banner);

        if (rs > 0) {
            return error(1, "保存成功！");
        } else {
            return error(0, "保存失败！");
        }
    }

    @RequestMapping(path = "/form", name = "查一个广告", produces = "text/html; charset=UTF-8")
    public ModelAndView form(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        Banner banner = bannerService.getBannerById(id);
        if (banner == null) {
            banner = new Banner();
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("banner", banner);
        return mv;
    }

    @RequestMapping(path = "/showOrHidden", name = "推荐显示|隐藏", produces = "application/json; charset=UTF-8")
    public Result showOrHidden(HttpServletRequest req) {
        int id = Integer.parseInt(req.getParameter("id"));
        int status = Integer.parseInt(req.getParameter("status"));
        System.out.println(id);
        System.out.println(status);
        int rs = bannerService.showOrHidden(id, status);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }

    }

    @RequestMapping(path = "/sortBanner", name = "广告的置顶|上移|下移", produces = "application/json; charset=UTF-8")
    public Result sortBanner(HttpServletRequest req) {
        int id = Integer.parseInt(req.getParameter("id"));
        String action = req.getParameter("action");
        Banner banner = bannerService.getBannerById(id);
        System.out.println("id:" + id + "======:" + banner);
        int rs = bannerService.sortBanner(banner, action);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/sortColumn", name = "推荐的置顶|上移|下移", produces = "application/json; charset=UTF-8")
    public Result sortColumn(HttpServletRequest req) {
        int id = Integer.parseInt(req.getParameter("id"));
        String action = req.getParameter("action");
        Column column = bannerService.getColumnById(id);
        int rs = bannerService.sortColumn(column, action);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }
}
